import argparse
import sys
import numpy as np
import gzip
import os

def parse_args():
    parser = argparse.ArgumentParser("")
    parser.add_argument("--key", type=str,required=False)
    parser.add_argument("--input",'-i', type=str,required=True)
    parser.add_argument("--output",'-o', type=str,required=True)
    return parser.parse_args()

def main():
    args=parse_args()
    if args.input.endswith(".gz"):
        input_file = gzip.open(args.input, 'rt')
    else:
        input_file = open(args.input, 'r')
    seen_indexes=set()
    reduce_indexes=set()
    if args.key is not None:
        with open(args.key, 'r') as key_file:
            for line in key_file:
                key = line.strip()
                reduce_indexes.add(key)

    filter=0
    repeat_num=0
    error_num=0
    with open(args.output, 'w') as output_file:
        for line in input_file:
            words=line.strip().split('\t')
            if len(words)!=11:
                print('words is less than 11 with {}'.format(words[0]), flush=True)
                error_num+=1
                continue
            index = '||'.join(words[:2])
            s_index='||'.join(words[:2]+[words[5]])
            if s_index not in seen_indexes and index not in reduce_indexes:
                pred_dorado=np.array([float(x) for x in words[6].split(",")])
                pred_deepsignal=np.array([float(x) for x in words[7].split(",")])
                log_1=0
                for i in range(pred_dorado.shape[0]):
                    if pred_deepsignal[i]==-1 and pred_dorado[i]!=-1:
                        log_1+=1
                if log_1/pred_dorado.shape[0]>0.133334:
                    filter+=1
                    continue 
                seq=words[3]
                pred_pos=np.array([int(x) for x in words[5].split(",")])
                flag=0
                for i in pred_pos:
                    if seq[i]!='C':
                        flag=1
                        error_num+=1
                        break
                if flag==1:
                    print(words[:3])
                    continue     
                output_file.write(line)
                seen_indexes.add(s_index)
            else:
                repeat_num+=1
    print('too few matched: {}'.format(filter),flush=True)
    print('repeat_num: {}'.format(repeat_num),flush=True)
    print('error_num: {}'.format(error_num),flush=True)


if __name__ == '__main__':
    sys.exit(main())